// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { show } = require("../game/alert");
const { shopData, player } = require("./gameData");

cc.Class({
    extends: cc.Component,

    properties: {
        money:cc.Label,
        percent:cc.Slider,
        count:cc.Integer,
        type:cc.String,
        shop:cc.Object,
        percent:cc.Float,
    },
    update () {
        this.money.string = parseInt(this.count*this.percent)+'元';
    },
    //显示菜单管理
    changePanel () {
        this.node.active = !this.node.active;//显示菜单管理
        this.money.string = this.count;

    },
    turnIn(){
        //获取当前的数值
        this.count = player.money;
        this.type = 'in';
    },
    turnOut(){
        
        this.count = this.shop.money;
        this.type = 'out';
    },
    ensure(){
        if(this.type == "in"){
            if(parseInt(this.count*this.percent)<=player.money){
                player.money-=parseInt(this.count*this.percent);
                this.shop.money+=parseInt(this.count*this.percent*0.9);
            }else{
                show('小子','钱没给够')
            }
           
        }else{
            if(parseInt(this.count*this.percent)<=this.shop.money){
                player.money+=parseInt(this.count*this.percent*0.9);
                this.shop.money-=parseInt(this.count*this.percent);
            }else{
                show('小子','钱没给够')
            }
            
        }
    },
    onSlider(target,i){
        // this.count = parseInt(this.count*target.progress);
        this.percent = target.progress;
        
    },
    onEnable () {
        this.turnIn()
        this.shop = shopData.shopList.find(val=>val.id == shopId);
        this.money.string = 0;
    },
    //生成菜单
    makeMenu () {
       
    },

});
